﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            var SelectString = "Select * from Students";
            var st = DbHelper.GetDataTable(SelectString);
            dataGridView1.DataSource = st;
            //不可编辑
            dataGridView1.ReadOnly = true;
            //选中一整行
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //去除多余行
            dataGridView1.AllowUserToAddRows = false;
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var st = string.Format("select * from Students where Name like '%{0}%'",name);
            var dt = DbHelper.GetDataTable(st);
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm editForm = new EditForm();
            var res = editForm.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var st = string.Format("select * from Students where Name like '%{0}%'", name);
                var dt = DbHelper.GetDataTable(st);
                dataGridView1.DataSource = dt;
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //判断选中的数据行是否大于0
            if (dataGridView1.SelectedRows.Count > 0)
            {
                //引用EditForm中的构造函数，使更新时更新界面默认有选中的数据
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
                var age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
                var score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;

                EditForm editForm = new EditForm(id, name, age, score);
                var res = editForm.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var newName = textBox1.Text;
                    var st = string.Format("select * from Students where Name like '%{0}%'", newName);
                    var dt = DbHelper.GetDataTable(st);
                    dataGridView1.DataSource = dt;
                }
            }
            else
            {
                MessageBox.Show("未选择任何数据行","提示");
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            //判断选定的行是否大于0
            if (dataGridView1.SelectedRows.Count > 0)
            {
                //获取行集合
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["Id"].Value;
                var res = MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {
                    var DeleteString = string.Format("delete from Students where Id={0}",id);
                    var ds = DbHelper.AddOrAddOrEditOrDeleteSave(DeleteString);
                    //删除之后数据如果不为0则不显示整个查找列表，只显示查找删除之后的界面
                    if (ds>0)
                    {
                        var name = textBox1.Text;
                        var st = string.Format("select * from Students where Name like '%{0}%'",name);
                        var dt = DbHelper.GetDataTable(st);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("没有任何选择","提示");
            }
        }
    }
}
